#include <Engine/StdAfx.h>
#include <Engine/Renderer/D3D11/TextureD3D11.h>
#include <Engine/GlobalEnv.h>
#include <Engine/IEngine.h>
#include <Engine/IRenderer.h>

using namespace fastbird;

//----------------------------------------------------------------------------
TextureD3D11* TextureD3D11::CreateInstance()
{
	TextureD3D11* pTexture = new TextureD3D11();
	return pTexture;
}

//----------------------------------------------------------------------------
TextureD3D11::TextureD3D11()
	: mTexture(0)
	, mSRView(0)
	, mSamplerState(0)
	, mSlot(0)
{
	mPixelFormat = PIXEL_FORMAT_R8G8B8A8;
}

//----------------------------------------------------------------------------
TextureD3D11::~TextureD3D11()
{
	SAFE_RELEASE(mSamplerState);
	SAFE_RELEASE(mSRView);
	SAFE_RELEASE(mTexture);	
}

//----------------------------------------------------------------------------
// ITexture
//----------------------------------------------------------------------------
bool TextureD3D11::IsReady() const
{
	return mSRView!=0;
}

//----------------------------------------------------------------------------
const Vec2I& TextureD3D11::GetSize() const
{
	return mSize;
}

//----------------------------------------------------------------------------
PIXEL_FORMAT TextureD3D11::GetFormat() const
{
	return mPixelFormat;
}

//----------------------------------------------------------------------------
void TextureD3D11::Bind()
{
	gEnv->pEngine->GetRenderer()->SetTexture(this, mBindingShader, mSlot);
}

//----------------------------------------------------------------------------
void TextureD3D11::SetSlot(int slot)
{
	mSlot = slot;
}

//----------------------------------------------------------------------------
void TextureD3D11::SetShaderStage(BINDING_SHADER shader)
{
	mBindingShader = shader;
}

//----------------------------------------------------------------------------
void TextureD3D11::SetSamplerDesc(const SAMPLER_DESC& desc)
{
	__super::SetSamplerDesc(desc);
	gEnv->pEngine->GetRenderer()->FindOrCreateSamplerState((ITexture*)this, desc);
}

//----------------------------------------------------------------------------
// OWN
//----------------------------------------------------------------------------
ID3D11Texture2D* TextureD3D11::GetHardwareTexture() const
{
	return mTexture;
}

//----------------------------------------------------------------------------
ID3D11ShaderResourceView* TextureD3D11::GetHardwareResourceView() const
{
	return mSRView;
}

//----------------------------------------------------------------------------
ID3D11SamplerState* TextureD3D11::GetSamplerState() const
{
	return mSamplerState;
}

//----------------------------------------------------------------------------
void TextureD3D11::SetHardwareTexture(ID3D11Texture2D* pTexture)
{
	SAFE_RELEASE(mTexture);
	mTexture = pTexture;
}
//----------------------------------------------------------------------------
void TextureD3D11::SetHardwareResourceView(ID3D11ShaderResourceView* pResourceView)
{
	SAFE_RELEASE(mSRView);
	mSRView = pResourceView;
}

//----------------------------------------------------------------------------
void TextureD3D11::SetSamplerState(ID3D11SamplerState* pSamplerState)
{
	SAFE_RELEASE(mSamplerState);
	mSamplerState = pSamplerState;
}